package com.cheyun.netsalev3


import android.app.Dialog
import android.text.TextUtils
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.cheyun.netsalev3.databinding.FragmentHomeMyBinding
import com.cheyun.netsalev3.repository.LoginRepository
import com.cheyun.netsalev3.utils.*
import com.cheyun.netsalev3.viewmodel.HomeMyFragmentViewModel
import com.cheyunkeji.er.utils.DensityUtil
import com.gyf.immersionbar.ImmersionBar
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlin.math.log

/**
 * 我的
 */
class HomeMyFragment : BaseFragment<FragmentHomeMyBinding>() {
    var loginData = MySharedPreferences.getLoginData()

    override fun initView() {
//        binding.txtPhone.setText("工作号 (尚未开通，请联系管理员)")
//        MySharedPreferences.getLoginData()?.member?.phoneX?.apply {
//            binding.txtPhone.setText("工作号 "+this)
//        }
        if(loginData?.member?.headimg!!.isNotEmpty()){
            Glide.with(this)
                    .load(loginData?.member?.headimg!!)
                    .transform(GlideRoundTransform(this.context, 5))
                    .placeholder(R.drawable.datuchicun)
                    .error(R.drawable.datuchicun)
                    .into(binding.ivUserFace)
        }


        binding.txtVersion.text  ="版本号："+ FunctionUtils.getCurrentVersion(this.context!!);
        LiveEventBus.get()
                .with("userinfoupdate", String::class.java)
                .observe(this, Observer {
                    loginData = MySharedPreferences.getLoginData()
                    binding.txtRealname.text = loginData?.member?.realname
                    binding.txtMobile.text = loginData?.member?.mobile
                    var groupname = loginData?.member?.groupname
                    if(groupname == null || groupname.isEmpty()){
                        if(loginData?.member?.isadmin == 1)
                            groupname = "(系统管理员)"
                    }else {
                        groupname = "("+groupname+")"
                    }
                    binding.txtGroup.text = groupname
                    if(loginData?.member?.headimg!!.isNotEmpty()){
                        Glide.with(this)
                                .load(loginData?.member?.headimg!!)
                                .transform(GlideRoundTransform(this.context, 5))
                                .placeholder(R.drawable.datuchicun)
                                .error(R.drawable.datuchicun)
                                .into(binding.ivUserFace)
                    }
                })

    }

    override fun initViewModel(): ViewModel? {
        return ViewModelProviders.of(this).get(HomeMyFragmentViewModel::class.java)
    }

    override fun initContentView(): Int {
      return R.layout.fragment_home_my
    }

    override fun initData() {
        binding.viewModel?.showLogOffCode?.observe(this, Observer { list ->
            showConfirmLogOff()
        })
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
        binding.viewModel?.getprdset()
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init()
        Log.e("HomeMyFragment onResume", userVisibleHint.toString())
        if (userVisibleHint && isBindingInit()) {
            login()
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (userVisibleHint && isBindingInit()) {
            login()
        }
    }

    private var loginRepository = LoginRepository()
    fun login(){
        var password: String = MySharedPreferences.getPassword()
        var mobile: String = MySharedPreferences.getMobile()
        loginRepository.simpleLogin(mobile, password, this::setData, this::loginFail)
    }

    fun setData() {
        binding.viewModel?.checkJiedai()
    }

    fun loginFail() {
    }

    fun showConfirmLogOff(){
        val bottomDialog = Dialog(this.activity, R.style.BottomDialog)
        val contentView = LayoutInflater.from(this.activity).inflate(R.layout.dialog_send_code, null)
        bottomDialog.setContentView(contentView)
        val params = contentView.layoutParams as ViewGroup.MarginLayoutParams
        params.width = this.getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(this.activity, 32f)
        params.bottomMargin = DensityUtil.dip2px(this.activity, 8f)
        contentView.layoutParams = params
        bottomDialog.window!!.setGravity(Gravity.CENTER_VERTICAL)
//        bottomDialog.window!!.setWindowAnimations(R.style.BottomDialog_Animation)
        bottomDialog.show()

        var etCode = contentView.findViewById<EditText>(R.id.et_code)
        var txtCancel = contentView.findViewById<TextView>(R.id.txt_cancel)
        var txtConfirm = contentView.findViewById<TextView>(R.id.txt_confirm)

        txtConfirm?.setOnClickListener {
            var code = etCode.text.toString()
            if(code.isEmpty()){
                ToastUtil.showToast("请输入短信验证码")
            }else{
                binding.viewModel?.confirmLogOff(it,code)
            }
        }

        txtCancel?.setOnClickListener {
            bottomDialog.dismiss()
        }
    }
}
